# -*- coding: UTF-8 -*-
'''
    自定义实现多属性枚举及相关操作类
'''
import collections

def create_enum(key_field=None, **kws):
    Enum =  type('Enum', (object,), kws)
    Enum.values = staticmethod(lambda:kws)
    if key_field:
        Enum.key_map = {getattr(kws[key], key_field):kws[key] for key in kws}
    return Enum

TcTicketUrl = collections.namedtuple('TcTicketUrl', 'cityCode proName cityName firstPageUrl nextPageUrl')
tcTicket001 = TcTicketUrl(cityCode=147, proName='安徽', cityName='阜阳', firstPageUrl='http://www.ly.com/scenery/scenerysearchlist_2_41__0__0__0_0_0.html', nextPageUrl='')
tcTicket002 = TcTicketUrl(cityCode=7, proName='安徽', cityName='安庆', firstPageUrl='', nextPageUrl='')
tcTicket003 = TcTicketUrl(cityCode=8, proName='安徽', cityName='铜陵', firstPageUrl='', nextPageUrl='')
tcTicket004 = TcTicketUrl(cityCode=9, proName='上海', cityName='上海', firstPageUrl='http://www.ly.com/scenery/scenerysearchlist_0_321__0_0_0_0_0_0_0.html?spm=7.68076534.9.1&track=fromlistBt', nextPageUrl='http://www.ly.com/scenery/NewSearchList.aspx?&action=getlist&page=1&kw=&pid=25&cid=321&cyid=0&sort=&isnow=0&spType=&lbtypes=&IsNJL=0&classify=0&grade=&dctrack=1%CB%871487158492907473%CB%871%CB%8723%CB%872473949062326092%CB%870&iid=0.13673976911291863')
tcTicket005 = TcTicketUrl(cityCode=309, proName='', cityName='株洲', firstPageUrl='http://www.ly.com/scenery/scenerysearchlist_0_0_%E6%A0%AA%E6%B4%B2_0_0_0_0_0_0_0.html?spm=7.68076534.9.1&track=fromlistBtn', nextPageUrl='')
tcTicket006 = TcTicketUrl(cityCode=318, proName='', cityName='永州', firstPageUrl='http://www.ly.com/scenery/scenerysearchlist_0_0_%E6%B0%B8%E5%B7%9E_0_0_0_0_0_0_0.html?spm=7.68076534.9.1&track=fromlistBtn', nextPageUrl='')
tcTicket007 = TcTicketUrl(cityCode=319, proName='', cityName='怀化', firstPageUrl='http://www.ly.com/scenery/scenerysearchlist_0_0_%E6%80%80%E5%8C%96_0_0_0_0_0_0_0.html?spm=7.68076534.9.1&track=fromlistBtn', nextPageUrl='')


TcTicketUrlEnum = create_enum(key_field='cityCode',
                              tcTicket001=tcTicket001,
                              tcTicket002=tcTicket002,
                              tcTicket003=tcTicket003,
                              tcTicket004=tcTicket004,
                              tcTicket005=tcTicket005,
                              tcTicket006=tcTicket006,
                              tcTicket007=tcTicket007)

# 根据城市code获得待爬城市首页URL以及接口URL
def selectUrlByCityCode(CityCode):
    for key in sorted(TcTicketUrlEnum.values(), key=lambda x:TcTicketUrlEnum.__dict__[x].cityCode):
        value = TcTicketUrlEnum.__dict__[key]
        if value.cityCode == CityCode:
            firstPageUrl = value.firstPageUrl
            nextPageUrl = value.nextPageUrl
            return (firstPageUrl, nextPageUrl)

# 根据城市code获得枚举所有属性信息
def selectEnumByCityCode(CityCode):
    try:
        return TcTicketUrlEnum.key_map[CityCode]
    except:
        pass

# 根据首页URL获得城市code码
def selectEnumByFirstPageUrl(FirstPageUrl):
    for key in sorted(TcTicketUrlEnum.values(), key=lambda x: TcTicketUrlEnum.__dict__[x].firstPageUrl):
        value = TcTicketUrlEnum.__dict__[key]
        if value.firstPageUrl == FirstPageUrl:
            cityCode = value.cityCode
            return cityCode

